package com.jrx.anytxn.investor.service.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.AmAccountMainInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.investor.entity.TlInvestorLoanChange;
import com.jrx.anytxn.investor.service.IInvestorLoanChangeService;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @program: anytxn-cf-transaction
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-06-08 10:44
 **/
@Service
public class InvestorLoanChangeServiceImpl implements IInvestorLoanChangeService {

    private static final Logger logger = LoggerFactory.getLogger(InvestorAccountServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;


    /**
     * 生成订单形态转移流水
     *
     * @param orgiLoan
     * @param businessDate
     * @return
     */
    @Override
    public TlInvestorLoanChange genarateLoanChange(AmLoanAccountInvestorInfo orgiLoan, Date businessDate) {

        TlInvestorLoanChange loanChange = new TlInvestorLoanChange();
        BeanMapping.copy(orgiLoan, loanChange);
        try {
            loanChange.setId(segmentService.getId(TransBizConstant.TL_INVESTOR_LOAN_CHANGE));
        } catch (SegmentException e) {
            logger.error("主键生成错误", e);
            throw new RuntimeException(TxnRespCode.ERROR.getUserTips());
        }
        loanChange.setBusinessDate(DateUtils.formatDate(businessDate, DateUtils.DATE_PATTERN_ANOTHER));
        loanChange.setSeqNo(SeqUtils.getSN());
        loanChange.setCreateTime(null);
        return loanChange;
    }

    @Override
    public TlInvestorLoanChange genarateLoanChange(String acctType, BigDecimal glAmt, String eventCode, String beforeStatus, String afterStatus) {
        TlInvestorLoanChange loanChange = new TlInvestorLoanChange();
        loanChange.setEventCode(eventCode);
        loanChange.setBeforeStatus(beforeStatus);
        loanChange.setAfterStatus(afterStatus);
        if (TransBizConstant.ACCOUNT_TYPE_05.equals(acctType) || TransBizConstant.ACCOUNT_TYPE_06.equals(acctType)) {
            loanChange.setPrincipalAmount(glAmt);
        } else if (TransBizConstant.ACCOUNT_TYPE_04.equals(acctType) || TransBizConstant.INTEREST_ACCRUAL_TYPE_66.equals(acctType)) {
            loanChange.setInterestAmount(glAmt);
        } else if (TransBizConstant.ACCOUNT_TYPE_11.equals(acctType) || TransBizConstant.INTEREST_ACCRUAL_TYPE_10.equals(acctType)) {
            loanChange.setPenaltyInterestAmount(glAmt);
        } else if (TransBizConstant.ACCOUNT_TYPE_12.equals(acctType) || TransBizConstant.INTEREST_ACCRUAL_TYPE_20.equals(acctType)) {
            loanChange.setCompoundInterestAmount(glAmt);
        }
        return loanChange;
    }

}
